package com.woniu.product.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.woniu.model.Result;
import com.woniu.product.dto.VenueAdminDto;
import com.woniu.product.dto.curriculum.CurriculumDto;
import com.woniu.product.form.LoadListClassForm;
import com.woniu.product.form.ScheduleClassForm;
import com.woniu.product.form.curriculum.AddCurriculumForm;
import com.woniu.product.form.curriculum.LoadCurriculumForm;
import com.woniu.product.model.Curriculum;
import com.woniu.product.param.LoadListClassParam;
import com.woniu.product.param.ScheduleClassParam;
import com.woniu.product.param.curriculum.AddCurriculumParam;
import com.woniu.product.param.curriculum.LoadCurriculumParam;
import com.woniu.product.service.CurriculumService;
import com.woniu.util.JwtUtil;
import com.woniu.util.RedisUtil;
import com.woniu.util.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年07月12日
 */
@RestController
@RequestMapping("/curriculum")
@Api(tags = "排课")
public class CurriculumController {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private CurriculumService curriculumService;
    @PostMapping("/scheduleClass")
    @ApiOperation("团课排课")
    public Result<?> scheduleClass(@RequestHeader String token, @RequestBody ScheduleClassForm form)throws Exception{
        String refreshToken = redisUtil.get(token);
//        Integer venueId = Integer.parseInt(JwtUtil.parseVenueAdminToken(refreshToken).get("venueId").toString());
        VenueAdminDto venueAdminDto = JSONUtil.toBean(JSONUtil.toJsonStr(JwtUtil.getVenueAdmin(refreshToken)),VenueAdminDto.class);
        ScheduleClassParam param = BeanUtil.toBean(form, ScheduleClassParam.class);
        param.setVenueId(venueAdminDto.getVenueId());
        curriculumService.scheduleClass(param);
        return Result.getResult().setMsg("排课成功");
    }

    @PostMapping("/addCurriculums")
    @ApiOperation("团课排课")
    public Result addCurriculums(@RequestBody List<AddCurriculumForm> forms)throws Exception{
        List<AddCurriculumParam> params = BeanUtil.copyToList(forms, AddCurriculumParam.class);
        curriculumService.addCurriculums(params);
        return Result.getResult().setMsg("排课成功");
    }

    @PostMapping("/notarizeClass")
    @ApiOperation("教练确认完成团课")
    public Result<?> notarizeClass(@RequestHeader String token,Integer classId)throws Exception{
        String refreshToken = redisUtil.get(token);
        VenueAdminDto venueAdminDto = JSONUtil.toBean(JSONUtil.toJsonStr(JwtUtil.getVenueAdmin(refreshToken)),VenueAdminDto.class);
        curriculumService.notarizeClass(venueAdminDto.getCoachId(), classId);
        return Result.getResult().setMsg("团课完成成功");
    }


    @PostMapping("/loadListClass")
    @ApiOperation("根据场馆id查询已排班的团课")
    public Result<?> loadListClass(@RequestHeader String token, @RequestBody LoadListClassForm form)throws Exception{
        String refreshToken = redisUtil.get(token);
//        Integer venueId = Integer.parseInt(JwtUtil.parseVenueAdminToken(refreshToken).get("venueId").toString());
        VenueAdminDto venueAdminDto = JSONUtil.toBean(JSONUtil.toJsonStr(JwtUtil.getVenueAdmin(refreshToken)),VenueAdminDto.class);

        LoadListClassParam param = BeanUtil.toBean(form, LoadListClassParam.class);
        param.setVenueId(venueAdminDto.getVenueId());
        List<Curriculum> curricula = curriculumService.loadListClass(param);
        return Result.getListResult(Curriculum.class).setCode(200).setData(curricula).setMsg("查询成功");
    }
    @ApiOperation("根据课程id查询排班信息")
    @GetMapping("/loadByClassId")
    public Result<List<Curriculum>>loadByClassId(Integer curriculumId)throws Exception{
        List<Curriculum> curricula = curriculumService.loadByClassId(curriculumId);
        return Result.getListResult(Curriculum.class).setData(curricula).setMsg("查询排班信息成功");
    }


    @PostMapping("/load")
    @ApiOperation("场馆查询课表")
    public Result load(@RequestBody LoadCurriculumForm form)throws Exception{
        LoadCurriculumParam param = BeanUtil.toBean(form, LoadCurriculumParam.class);
        PageInfo<CurriculumDto>  curriculumDtoPageInfo= curriculumService.loadCurriculum(param);
        return Result.getResult().setData(curriculumDtoPageInfo).setMsg("查询成功");
    }

}

